package com.wrupple.muba.common.client.state;

import java.util.List;

import org.cobogw.gwt.user.client.CSS;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Widget;
import com.wrupple.muba.common.shared.StateTransition;
import com.wrupple.muba.widget.client.widgets.panels.CenteredPanel;
import com.wrupple.muba.widget.client.widgets.panels.LayoutGrid;
import com.wrupple.muba.widget.client.widgets.panels.PanelWithToolbar;

public abstract class GridSelectionState<O> implements
		IconSelectionTask<O> {

	
	
	public interface GridPlaceSelectionStateDelegate<T> extends Delegate<T>{

		LayoutGrid getLayoutGrid();

		int getGridWidth();
		
	}
	
	protected GridPlaceSelectionStateDelegate<O> delegate;
	protected PanelWithToolbar main;
	protected LayoutGrid grid;

	protected GridSelectionState(
			GridPlaceSelectionStateDelegate<O> delegate) {
		super();
		this.delegate = delegate;
		main = new PanelWithToolbar();
		this.grid = delegate.getLayoutGrid();
		CenteredPanel panel = new CenteredPanel(grid, delegate.getGridWidth());
		main.getContentContainer().setWidget(panel);
	}


	@Override
	public void start(List<O> parameter, StateTransition<List<O>> onDone,
			EventBus bus) {
			List<GridElement> elements = delegate.getElements(parameter,onDone);
			
			
			for(GridElement element : elements){
				grid.add(element.asWidget());
			}
			grid.forceLayout();
			
			for(GridElement element : elements){
				grid.positionElement(element);
			}
			
			grid.animate(1000);
			
			CSS.setProperty(grid, "overflowY", "auto");
			CSS.setProperty(grid, "overflowX", "hidden");
	}
	
	@Override
	public final Widget asWidget() {
		return main;
	}
	



}
